<?php

class tr_barang_masuk extends record {

    function __construct() {
        parent::__construct();
    }

    function getLastIdUseArr($arrW) {
        $this->get('id', $arrW, 1);
        $w = $this->execute();
        return $w['result'][0]['id'];
    }
    function createTransNum() {
        $this->namaTabel = 'barang_masuk';
        $this->get('no_tr', array(), 1, 0, 'waktu', 'DESC');
        $ws = $this->execute();
        if ($ws['success'] == true) {
            if ($ws['total'] > 0) {
                $noTr = explode('.', $ws['result'][0]['no_tr']);
                $e = (int) $noTr[3] + 1;
                switch (strlen($e)) {
                    case 1:
                        $eT = '00' . $e;

                        break;
                    case 2:
                        $eT = '0' . $e;

                        break;
                    case 3:
                        $eT = $e;
                        break;
                    default:
                        $eT = '000';
                        break;
                };
                $noTrEnd = $noTr[0] . "." . $noTr[1] . "." . $noTr[2] . '.' . $eT;
            } else {
                $noTrEnd = 'JMC.BM.' . date('Ym') . '001';
            };
            return array('success' => true, 'result' => $noTrEnd);
        } else {
            return $ws;
        }
    }

    function addDetail($idP, $jml, $reset = NULL) {
        $strQ = "SELECT p.nama, k.nama AS kategori FROM produk p 
		LEFT JOIN kategori k
		ON k.id = p.idkategori

		WHERE p.id = '" . $idP . "'
                    LIMIT 1
		";
        $qRet = array();
        $this->query($strQ);
        $re = $this->execute();
        if ($re['success'] == true) {
            if ($re['total'] = 0) {
                $qRet['success'] = false;
                $qRet['error_msg'] = "Barang Yang Dimasukkan Tidak Ada Dalam Data";
            } else {
                $qRet['success'] = true;
                $dtre = $re['result'][0];
                $dtSes['nama'] = $dtre['nama'] . ' - [ ' . $dtre['kategori'] . ' ]';
                $dtSes['jumlah'] = $jml;
                $dtSes['idproduk'] = $idP;
                if (isset($reset) && $reset == 1) {
                    $sesData = $this->detailTemp();
                    foreach ($sesData['result'] as $key => $value) {
                        if ($value['idproduk'] == $idP) {
                            $resetKey = $key;
                        }
                    };
                    if (isset($resetKey)) {
                        $_SESSION['detail_barang_masuk'][$resetKey] = $dtSes;
                    }
                } else {
                    $_SESSION['detail_barang_masuk'][] = $dtSes;
                }
            }
        } else {
            $qRet = $re;
        };
        return $qRet;
    }

    function cekSesDetail($idP) {
        //deprecated?
        $data = isset($_SESSION['detail_barang_masuk']) ? $_SESSION['detail_barang_masuk'] : array();
        $ada = false;
        foreach ($data as $st) {
            if (isset($st['idproduk']) && $st['idproduk'] == $idP) {
                $ada = true;
            }
        };
        return $ada;
    }

    function detailTemp() {
        $data = isset($_SESSION['detail_barang_masuk']) ? $_SESSION['detail_barang_masuk'] : array();
        $ret = array('total' => count($data), 'result' => $data);
        return $ret;
    }

}

 $objTransBarangMasuk = new tr_barang_masuk();